package com.gm.sfc.logic;

import static com.gm.sfc.field.DbColumnField.*;
import static com.gm.sfc.util.Utility.*;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.res.AssetManager;

import com.gm.sfc.field.IField;
import com.gm.sfc.util.LogUtil;

/**
 * CSVロジック
 * @author $Author:$
 * @version $Revision:$
 */
public class CsvLogic extends ALogic {
    private static final String TAG = "CsvLogic";

    public static final String USER = "user.csv";

    public static final String WEAPON = "weapon.csv";
    public static final String HELMET = "helmet.csv";
    public static final String ARMOR = "armor.csv";
    public static final String ORNAMENT = "ornament.csv";
    public static final String MINERIA = "mineria.csv";

    /** CsvLogic */
    private static CsvLogic sCsvLogic;
    /** Context */
    private Context mContext;

    /**
     * Constructor
     * @param context Context
     */
    public CsvLogic(Context context) {
        mContext = context;
    }

    /**
     * Get an instance of CsvLogic
     * @param context Context
     * @return instance of CsvLogic
     */
    public static CsvLogic getInstance(Context context) {
        if (sCsvLogic == null) {
            sCsvLogic = new CsvLogic(context);
        }
        return sCsvLogic;
    }

    /**
     * CSVファイルを解析する
     * @param fileName 解析するファイル名
     * @return 解析したデータリスト
     */
    public ArrayList<HashMap<IField, Object>> read(String fileName) {
        return read(fileName, "SJIS");
    }

    /**
     * CSVファイルを解析する
     * @param fileName 解析するファイル名
     * @param encode エンコード方式
     * @return 解析したデータリスト
     */
    public ArrayList<HashMap<IField, Object>> read(String fileName, String encode) {
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        try {
            AssetManager assetManager = mContext.getAssets();
            inputStream = assetManager.open(fileName);
            inputStreamReader = new InputStreamReader(inputStream, encode);
            bufferedReader = new BufferedReader(inputStreamReader);
            if (fileName.equals(WEAPON)) {
                return parserWeapon(bufferedReader);
            } else if (fileName.equals(HELMET)) {
                return parserWear(bufferedReader);
            } else if (fileName.equals(ARMOR)) {
                return parserWear(bufferedReader);
            } else if (fileName.equals(ORNAMENT)) {
                return parserOrnament(bufferedReader);
            } else if (fileName.equals(MINERIA)) {
                return parserMineria(bufferedReader);
            } else {
                LogUtil.logD(TAG, "No such file name " + fileName);
            }
            return null;
        } catch (Exception e) {
            return null;
        } finally {
            try {
                bufferedReader.close();
            } catch (Exception e) {
            }
            try {
                inputStreamReader.close();
            } catch (Exception e) {
            }
            try {
                inputStream.close();
            } catch (Exception e) {
            }
        }
    }

    private ArrayList<HashMap<IField, Object>> parserWeapon(BufferedReader bufferedReader) throws Exception {
        HashMap<IField, Object> item;
        ArrayList<HashMap<IField, Object>> resultList = new ArrayList<HashMap<IField, Object>>();

        // 解析
        bufferedReader.readLine(); // 最初の１行は含めない
        while (true) {
            String line = bufferedReader.readLine();
            if (isEmpty(line)) {
                break;
            }
            if (line.indexOf(",") != -1) {
                String[] array = line.split(",");
                if (array.length == 20) {
                    item = new HashMap<IField, Object>();
                    item.put(type, array[0]);
                    item.put(name, array[1]);
                    item.put(rarelity, array[2]);
                    item.put(offence, array[3]);
                    item.put(strength, array[4]);
                    item.put(vaitality, array[5]);
                    item.put(wiseness, array[6]);
                    item.put(dexterity, array[7]);
                    item.put(speed, array[8]);
                    item.put(fire, array[9]);
                    item.put(water, array[10]);
                    item.put(wind, array[11]);
                    item.put(ground, array[12]);
                    item.put(light, array[13]);
                    item.put(darkness, array[14]);
                    item.put(sell_price, array[15]);
                    item.put(data1, array[16]);
                    item.put(data2, array[17]);
                    item.put(data3, array[18]);
                    item.put(condition, array[19]);
                    resultList.add(item);
                }
            }
        }
        return resultList;
    }

    private ArrayList<HashMap<IField, Object>> parserWear(BufferedReader bufferedReader) throws Exception {
        HashMap<IField, Object> item;
        ArrayList<HashMap<IField, Object>> resultList = new ArrayList<HashMap<IField, Object>>();

        // 解析
        bufferedReader.readLine(); // 最初の１行は含めない
        while (true) {
            String line = bufferedReader.readLine();
            if (isEmpty(line)) {
                break;
            }
            if (line.indexOf(",") != -1) {
                String[] array = line.split(",");
                if (array.length == 20) {
                    item = new HashMap<IField, Object>();
                    item.put(type, array[0]);
                    item.put(name, array[1]);
                    item.put(rarelity, array[2]);
                    item.put(defense, array[3]);
                    item.put(strength, array[4]);
                    item.put(vaitality, array[5]);
                    item.put(wiseness, array[6]);
                    item.put(dexterity, array[7]);
                    item.put(speed, array[8]);
                    item.put(fire, array[9]);
                    item.put(water, array[10]);
                    item.put(wind, array[11]);
                    item.put(ground, array[12]);
                    item.put(light, array[13]);
                    item.put(darkness, array[14]);
                    item.put(sell_price, array[15]);
                    item.put(data1, array[16]);
                    item.put(data2, array[17]);
                    item.put(data3, array[18]);
                    item.put(condition, array[19]);
                    resultList.add(item);
                }
            }
        }
        return resultList;
    }

    private ArrayList<HashMap<IField, Object>> parserOrnament(BufferedReader bufferedReader) throws Exception {
        HashMap<IField, Object> item;
        ArrayList<HashMap<IField, Object>> resultList = new ArrayList<HashMap<IField, Object>>();

        // 解析
        bufferedReader.readLine(); // 最初の１行は含めない
        while (true) {
            String line = bufferedReader.readLine();
            if (isEmpty(line)) {
                break;
            }
            if (line.indexOf(",") != -1) {
                String[] array = line.split(",");
                if (array.length == 19) {
                    item = new HashMap<IField, Object>();
                    item.put(type, array[0]);
                    item.put(name, array[1]);
                    item.put(rarelity, array[2]);
                    item.put(hit_point, array[3]);
                    item.put(magic_point, array[4]);
                    item.put(offence, array[5]);
                    item.put(defense, array[6]);
                    item.put(strength, array[7]);
                    item.put(vaitality, array[8]);
                    item.put(wiseness, array[9]);
                    item.put(dexterity, array[10]);
                    item.put(speed, array[11]);
                    item.put(fire, array[12]);
                    item.put(water, array[13]);
                    item.put(wind, array[14]);
                    item.put(ground, array[15]);
                    item.put(light, array[16]);
                    item.put(darkness, array[17]);
                    item.put(sell_price, array[18]);
                    resultList.add(item);
                }
            }
        }
        return resultList;
    }

    private ArrayList<HashMap<IField, Object>> parserMineria(BufferedReader bufferedReader) throws Exception {
        HashMap<IField, Object> item;
        ArrayList<HashMap<IField, Object>> resultList = new ArrayList<HashMap<IField, Object>>();

        // 解析
        bufferedReader.readLine(); // 最初の１行は含めない
        while (true) {
            String line = bufferedReader.readLine();
            if (isEmpty(line)) {
                break;
            }
            if (line.indexOf(",") != -1) {
                String[] array = line.split(",");
                if (array.length == 6) {
                    item = new HashMap<IField, Object>();
                    item.put(type, array[0]);
                    item.put(name, array[1]);
                    item.put(rarelity, array[2]);
                    item.put(skill, array[3]);
                    item.put(cost, array[4]);
                    item.put(sell_price, array[5]);
                    resultList.add(item);
                }
            }
        }
        return resultList;
    }
}
